import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:get/get.dart';
import 'package:syn_4s/core/extension/build_context_extension.dart';
import 'package:syn_4s/core/utils/foreground_task_util.dart';
import 'package:syn_4s/core/viewmodel/google_map_view_model.dart';
import 'package:syn_4s/core/viewmodel/main_view_model.dart';
import 'package:syn_4s/ui/pages/main/main_content.dart';
import 'package:syn_4s/ui/widgets/change_language.dart';
import 'package:syn_4s/ui/widgets/custom_ui.dart';
import 'package:syn_4s/ui/widgets/main_drawer.dart';

import '../../../core/constants/event_code.dart';
import '../../../core/utils/event_bus.dart';
import '../../../core/utils/mqtt_manager.dart';

class MainScreen extends StatefulWidget {
  static const String routeName = "/main";

  const MainScreen({super.key});

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen>
    with AutomaticKeepAliveClientMixin {

  @override
  void initState() {
    super.initState();
    FlutterBackgroundService().invoke('setAsBackground');
    MainViewModel vm = MainViewModel();
    vm.initData(context);
    Get.put(vm);
    listenInt((event) {
      if (event == eventSite) {
        // manager.closeConnect();
        // manager.connect();
        setState(() {});
      } else if (event == eventLanguage) {
        setState(() {});
      }
    });
    // manager.connect();
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ChangeLanguage(
      child: Scaffold(
        drawer: const MainDrawer(),
        appBar: getAppBar(context.l10n.device_status),
        body: const MainContent(),
      ),
    );
  }
}
